package eu.ydp.mathplayer.editor.controller;

import java.util.Vector;

import com.mathplayer.player.model.Token;
import com.mathplayer.player.model.interaction.EmptySpace;
import com.mathplayer.player.model.layoutschematas.FenceType;
import com.mathplayer.player.model.layoutschematas.MFenced;
import com.mathplayer.player.model.layoutschematas.MFraction;
import com.mathplayer.player.model.layoutschematas.MRoot;
import com.mathplayer.player.model.layoutschematas.MRow;
import com.mathplayer.player.model.layoutschematas.MSubSup;
import com.mathplayer.player.model.layoutschematas.MUnderOver;
import com.mathplayer.player.model.tokens.MIdentifier;
import com.mathplayer.player.model.tokens.MOperator;
import com.mathplayer.player.model.tokens.MStringLiteral;

public abstract class TokensFactory {

	public static Vector<Token> createToken(InsertTokenType insertTokenType, Object[] args){
		Token token = null;
		
		Vector<Token> tokens0 = new Vector<Token>();
		tokens0.add(new EmptySpace());
		MRow row0 = new MRow(tokens0);
		
		Vector<Token> tokens1 = new Vector<Token>();
		tokens1.add(new EmptySpace());
		MRow row1 = new MRow(tokens1);

		Vector<Token> tokens2 = new Vector<Token>();
		tokens2.add(new EmptySpace());
		MRow row2 = new MRow(tokens2);
		
		Vector<Token> tokens3 = new Vector<Token>();
		tokens3.add(new EmptySpace());
		MRow row3 = new MRow(tokens3);
		MFenced fenced3 = new MFenced(row3, FenceType.ROUND, FenceType.ROUND);
		
		if (insertTokenType == InsertTokenType.FRACTION){			
			token = new MFraction(row0, row1);
		} else if (insertTokenType == InsertTokenType.SUBSUP){
			token = new MSubSup(row0, row1, row2);
		} else if (insertTokenType == InsertTokenType.SUB){
			token = new MSubSup(row0, row1, null);
		} else if (insertTokenType == InsertTokenType.SUP){
			token = new MSubSup(row0, null, row2);
		} else if (insertTokenType == InsertTokenType.UNDEROVER){
			token = new MUnderOver(row0, row1, row2);
		} else if (insertTokenType == InsertTokenType.UNDER){
			token = new MUnderOver(row0, row1, null);
		} else if (insertTokenType == InsertTokenType.OVER){
			token = new MUnderOver(row0, null, row2);
		} else if (insertTokenType == InsertTokenType.SQRT){
			token = new MRoot(row0, null);
		} else if (insertTokenType == InsertTokenType.ROOT){
			token = new MRoot(row0, row1);
		} else if (insertTokenType == InsertTokenType.FENCE_ROUND){
			token = new MFenced(row0, FenceType.ROUND, FenceType.ROUND);
		} else if (insertTokenType == InsertTokenType.FENCE_SQUARE){
			token = new MFenced(row0, FenceType.SQUARE, FenceType.SQUARE);
		} else if (insertTokenType == InsertTokenType.FENCE_CURLY){
			token = new MFenced(row0, FenceType.CURLY, FenceType.CURLY);
		} else if (insertTokenType == InsertTokenType.FENCE_ANGLE){
			token = new MFenced(row0, FenceType.ANGLE, FenceType.ANGLE);
		} else if (insertTokenType == InsertTokenType.FENCE_ROUND_SQUARE){
			token = new MFenced(row0, FenceType.ROUND, FenceType.SQUARE);
		} else if (insertTokenType == InsertTokenType.FENCE_SQUARE_ROUND){
			token = new MFenced(row0, FenceType.SQUARE, FenceType.ROUND);
		} else if (insertTokenType == InsertTokenType.FENCE_ROUND_ANGLE){
			token = new MFenced(row0, FenceType.ROUND, FenceType.ANGLE);
		} else if (insertTokenType == InsertTokenType.FENCE_ANGLE_ROUND){
			token = new MFenced(row0, FenceType.ANGLE, FenceType.ROUND);
		} else if (insertTokenType == InsertTokenType.IDENTIFIER){
			token = new MIdentifier((String)args[0]);
		} else if (insertTokenType == InsertTokenType.OPERATOR){
			token = new MOperator((String)args[0]);
		} else if (insertTokenType == InsertTokenType.SIN){
			String arg = (String)args[0];
			Vector<Token> tokens = new Vector<Token>();
			tokens.add(new MStringLiteral(arg.substring(0, 1)));
			tokens.add(new MStringLiteral(arg.substring(1, 2)));
			tokens.add(new MStringLiteral(arg.substring(2, 3)));
			tokens.add(fenced3);
			return tokens;
		} else if (insertTokenType == InsertTokenType.SIN_P){
			String arg = (String)args[0];

			Vector<Token> baseRowTokens = new Vector<Token>();
			baseRowTokens.add(new MStringLiteral(arg.substring(0, 1)));
			baseRowTokens.add(new MStringLiteral(arg.substring(1, 2)));
			baseRowTokens.add(new MStringLiteral(arg.substring(2, 3)));
			MRow baseRow = new MRow(baseRowTokens);
			MSubSup subsup = new MSubSup(baseRow, null, row0);
			
			Vector<Token> tokens = new Vector<Token>();
			tokens.add(subsup);
			tokens.add(fenced3);
			return tokens;
		}
		
		Vector<Token> tokens = new Vector<Token>();
		tokens.add(token);
		return tokens;
	}
}
